package com.activities;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import com.Adapters.Items.RowType;
import com.Animations.AnimUtil;
import com.Downloaders.ProfilePictureDownloader;
import com.Downloaders.ProfilePictureDownloader.onProfilePictureDownloaded;
import com.Downloaders.ThumbDownloader;
import com.Downloaders.ThumbDownloader.ThumbnailCompleted;
import com.Managers.PictureManagerClass;
import com.Managers.ThemeManager;
import com.TextFormat.UtilFonts;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.shareup.R;


public class ActivityMap extends FragmentActivity {

    private ImageView backButton;

    private String dir;

    private final OnClickListener gotoLocation = new OnClickListener() {

        @Override
        public void onClick(View v) {

            moveCamera(point);

        }
    };

    private boolean isMine;

    private Double latitud;

    private TextView location;

    private ImageView locationButton;

    private Double longitud;

    private GoogleMap map;

    private final int mapzoom = 13;

    private ImageView markerPicture;

    private View markerView;

    private String owner;

    private String paththumb;

    private LatLng point;

    private TextView title;

    private int Type;

    public void moveCamera(LatLng punto) {

        map.animateCamera(CameraUpdateFactory.newLatLngZoom(punto, mapzoom));

    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            //
            return true;
        }
        //
        return super.onKeyUp(keyCode, event);
    }

    private void addMark(Bitmap mark, LatLng point) {

        mark = Bitmap.createScaledBitmap(mark, mark.getWidth() / 2, mark.getHeight() / 2, false);
        BitmapDescriptorFactory.defaultMarker();

        markerPicture.setImageBitmap(mark);
        map.addMarker(new MarkerOptions().position(point).icon(BitmapDescriptorFactory.fromBitmap(PictureManagerClass.createDrawableFromView(this, markerView))));

        moveCamera(point);
    }

    private void addPoint(final LatLng point, String Url) {

        if (Url.equals("null") || Url.isEmpty())
            exit();

        if (Type == RowType.PICTURE_POST.ordinal()) {
            ThumbDownloader td = new ThumbDownloader(this, Url);
            td.setCompleteBitmap(new ThumbnailCompleted() {

                @Override
                public void Completed(Bitmap thumb) {

                    addMark(thumb, point);
                }
            });
            td.execute();

        }
        else
            if (Type == RowType.TEXT_POST.ordinal()) {
                ProfilePictureDownloader pd = new ProfilePictureDownloader();
                pd.setCallbackProfilePicture(new onProfilePictureDownloaded() {

                    @Override
                    public void completed(Bitmap pictureProfile) {

                        addMark(pictureProfile, point);

                    }
                });
                pd.execute(Url);
            }

    }

    private void exit() {

        finish();
        AnimUtil.getActivityTransition(this, false);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ThemeManager.setTheme(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            latitud = extras.getDouble("latitud", 0);
            longitud = extras.getDouble("longitud", 0);
            paththumb = extras.getString("picture");
            owner = extras.getString("owner");
            dir = extras.getString("local");
            isMine = extras.getBoolean("mine", false);
            Type = extras.getInt("type");

        }
        else {
            exit();
        }

        int tit = R.string.string_picture_owner;

        title = (TextView) findViewById(R.id.ac_map_title_actionbar_main);
        title.setText(String.format(getString(tit), owner));
        //
        markerView = LayoutInflater.from(this).inflate(R.layout.map_picture_marker, null);
        markerPicture = (ImageView) markerView.findViewById(R.id.marker_image);

        if (Type == RowType.TEXT_POST.ordinal())
            markerPicture.setBackgroundResource(R.drawable.background_profile_picture);
        else
            markerPicture.setBackgroundResource(R.drawable.background_picture_map);

        //
        location = (TextView) findViewById(R.id.ac_map_location_text);
        location.setText(dir);
        location.setOnClickListener(gotoLocation);
        //
        backButton = (ImageView) findViewById(R.id.button_back);
        backButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                exit();

            }
        });
        //
        locationButton = (ImageView) findViewById(R.id.acrop_add_location);
        locationButton.setOnClickListener(gotoLocation);
        //
        UtilFonts.setTextRegularFont(title);
        UtilFonts.setTextRegularFont(location);

        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        point = new LatLng(latitud, longitud);
        addPoint(point, paththumb);
    }
}
